package manage.manager;


import core.constants.Constant;
import core.redis.RedisService;
import manage.domain.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import java.util.concurrent.TimeUnit;

@Component
public class UserCacheManage {
    @Autowired
    private RedisService redisService;

    public void updateStatus(Long userId, Integer status) {
        String userKey = getUserKey(userId);
        if(!redisService.hasKey(userKey)){
            return;
        }
        User cacheObject = redisService.getCacheObject(userKey, User.class);
        if(cacheObject == null){
            return;
        }
        cacheObject.setStatus(status.byteValue());
        redisService.setCacheObject(userKey, cacheObject,
                Constant.USER_EXP, TimeUnit.MINUTES);
    }

    private String getUserKey(Long userId){
        return Constant.USER_CACHE + userId;
    }
}
